package com.loan.controller;
import java.util.Date;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.loan.model.Company;
import com.loan.model.Data;
import com.loan.model.Staff;
import com.loan.model.User;
import com.loan.service.StaffService;
import com.loan.service.UserService;
import com.loan.util.Json;
import com.loan.util.MD5Util;
import com.loan.util.Str;

/**
 *  员工控制器
 * @since 2017-06-06
 * @author 无处不在的二叔
 */
public class StaffController extends Controller {
	private static UserService user = new UserService();
	private static StaffService staff = new StaffService();

	/**
	 *  获取员工信息
	 */ 
	public void info() {
		Staff s =  Staff.dao.findById(getParaToInt("id",0));
		renderJson(Json.ok("数据获取成功！",s));
	}
	
	/**
	 * 员工列表
	 */
	public void list() { 
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		Map<String,String> params = Data.flat(getParaMap());
		Page<Staff> p = staff.list(page,size,params);
		renderJson(Json.ok( "数据获取成功！", Data.put("page",p)));
	}	
	
	/**
	 * 员工信息编辑
	 */
	public void edit() { 
		int userId = getParaToInt("userId",0);
		String mobile = getPara("mobile");
		String realName = getPara("realName",mobile);
		String role = getPara("role");
		String workcode = getPara("workcode");
		String password = getPara("password");
		int cid = getAttr("cid");
		
		// 新建员工判断
		if(userId==0) {
			if(Str.eq("agent_leader", role)) {
				if(staff.list(1,1,Data.putStr("role",role)).getList().size()>0) {
					renderJson(Json.error("当前客服组已有组长！"));
					return ;
				}
			} 
			User u1 = user.findByMobile(cid, mobile);
			if(u1==null) {
				u1 = new User();
				u1.setRealName(realName)
					.setMobile(mobile)
					.setPassword(MD5Util.encrypt(password))
					.setCid(cid)
					.setCname(Company.dao.findById(cid).getName())
					.setIsStaff(1)
					.setStatus("正常")
					.setAgentId(0)
					.save();
			}
			userId = u1.getId();
		}
		
		Staff s = staff.findByWorkcode(workcode);
		if(s!=null && s.getUserId()!=userId) {
			renderJson(Json.error("此工号已存在！"));
			return ;
		} 
		
		if(userId>0 && Str.nb(realName) && Str.nb(mobile)){
			User u = User.dao.findById(userId);
			int id = getParaToInt("id",0);
			s = id>0 ? Staff.dao.findById(id) : new Staff();
			s.setCid(u.getCid());  
			s.setUserId(userId);
			s.setRealName(realName);
			s.setMobile(mobile);
			s.setWorkcode(workcode);
			s.setRole(role);
			s.setCid(cid);
			
			// 刷新手机号 & 姓名
			u.setRealName(realName).setMobile(mobile).update();
			
			// 修改密码
			if(Str.nb(password)) {
				s.setPassword(MD5Util.encrypt(password));
			}
			 
			if(id>0 ? s.setUpdateTime(new Date()).update() : s.setCreateTime(new Date()).save()){
				renderJson(Json.ok("员工信息编辑成功！")); 
			} else {
				renderJson(Json.error("员工信息编辑失败！"));
			}
		}else{
			renderJson(Json.error("员工信息不可为空！"));
		}
	}	
	
	/**
	 * 员工删除
	 */
	public void del() {
		Staff s = Staff.dao.findById(getParaToInt("id",0));
		if(s!=null){
			s.delete();
			renderJson(Json.ok("删除成功！"));
		}
	}
}
 